#include "stdafx.h"
#include "Thread.h"

namespace StdTools
{
	namespace Threads
	{
#ifdef WIN
		DWORD WINAPI ProxyFunc(void * p_param)
#else
		void * ProxyFunc(void *p_param)
#endif
		{
			Runnable * runnable = (Runnable *)p_param;
			runnable->Run();
			//delete runnable;
			return 0;
		}

		Thread::Thread(Runnable * runnable)
		{
			this->runnable = runnable;
		}

		Thread::~Thread(void)
		{
		}

		ThreadID Thread::getID()
		{
			return this->id;
		}

		void Thread::start()
		{				
#ifdef WIN
			DWORD ident;
			this->id = CreateThread(NULL,0,ProxyFunc,this->runnable,0,&ident);
#else
			pthread_create(&this->id, 0, ProxyFunc, this->runnable);
#endif
		}

		void Thread::join(Thread * thread)
		{
#ifdef WIN
			WaitForSingleObject(thread->getID(),INFINITE);
#else
			pthread_join(thread->getID(),NULL);
#endif
		}
	}
}